package cn.sumpu.app.wlcity.web.mvc.hotspring;

import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.sumpu.app.wlcity.biz.VoteBiz;
import cn.sumpu.app.wlcity.domain.Vote;
import cn.sumpu.app.wlcity.domain.VoteItem;
import cn.sumpu.app.wlcity.exception.AppBusinessException;
import cn.sumpu.app.wlcity.util.Common;
@Controller
public class VotespringController{
	@Autowired
	private VoteBiz voteBiz;
	//投票首页
	@RequestMapping(value = "/vote.htm")
	public String vote(HttpServletRequest request, Integer page,String app) {
		try {
			if (page == null) 
				page = 1; 
			app=Common.APP_SPRING;
			List<Vote> voteList = voteBiz.getFrontVoteList(page,app);
			request.setAttribute("voteList", voteList);
		} catch (AppBusinessException e) {
			e.printStackTrace();
			return "hotspring/error";
		}
		return "hotspring/vote/vote";
	}

	// 对列表项投票
	@RequestMapping(value = "/voteIt.htm")
	public String voteIt(HttpServletRequest request,
			HttpServletResponse response, String voteItemId, String phone,String app) {
		try {
			response.setContentType("text/html; charset=UTF-8"); 
			response.setHeader("Cache-Control", "no-cache");  
			PrintWriter pw = response.getWriter();
			Boolean voteResult = voteBiz.insertVoteUsePhone(voteItemId, phone,app);
			if (voteResult == true)
				pw.write("success");
			else
				pw.write("failure");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	//爽投票
	@RequestMapping(value = "/getVoteItemList.htm")
	public String getVoteItemList(HttpServletRequest request, String voteId,String voteTitle,Integer page) {
		try {
			List<VoteItem> voteItemList = voteBiz.getVoteItemList(voteId,page);  
			request.setAttribute("voteItemList", voteItemList);
			request.setAttribute("voteTitle", voteTitle);
			request.setAttribute("voteId", voteId);
		} catch (AppBusinessException e) {
			e.printStackTrace();
		}
		return "hotspring/vote/voteItemList";
	}
	//投一票
	@RequestMapping(value = "/getVoteItem.htm")
	public String getVoteItemById(HttpServletRequest request, String voteItemId,String voteItemTitle) {
		try {
			VoteItem voteItem = voteBiz.getVoteItemById(voteItemId);
			String voteId=voteItem.getVoteId();
			Vote vote=voteBiz.getVoteById(voteId);
			request.setAttribute("voteId", voteItem.getVoteId());
			request.setAttribute("voteTitle", vote.getTitle());
			request.setAttribute("voteItem", voteItem);
		} catch (AppBusinessException e) {
			e.printStackTrace();
		}
		return "hotspring/vote/voteItemDetail";
	}
}
